﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Age.Qna.Site.Mvc.Util
{
  public class CategoryTree
  {

    #region - Variables -
    private bool _itemWithCheckbox = false;
    private int _treeLevel = 0;
    private int _itemNumber = 0;
    private string _categoryUrl = "#";
    #endregion

    #region - Properties -
    public bool UseCheckBox { set { _itemWithCheckbox = value; } }
    public string CategoryURL { set { _categoryUrl = value; } } 
    //TODO[P1]: Ability to update in JS the CategoryURL based on <a> ID or class. 
    #endregion

    #region - Constructor -
    public CategoryTree()
    {

    }
    #endregion

    #region - GetCategoryXML -
    /// <summary>
    /// Returns an XML object of all categories and its children
    /// </summary>
    /// <returns></returns>
    public string GetCategoryXML()
    {
      Qnawsvc.QnaWSvcClient client = new Qnawsvc.QnaWSvcClient("ws");
      Object o = client.GetCategoryTree();
      client.Close();

      string tree = Util.Xml.XmlUtils.ToXML(o);

      return tree;
    }
    #endregion

    #region - RenderCategoryUL -
    /// <summary>
    /// Returns an UL object with all categories and its children in it.
    /// </summary>
    /// <returns></returns>
    public string RenderCategoryUL()
    {
      Qnawsvc.QnaWSvcClient client = new Qnawsvc.QnaWSvcClient("ws");
      IList<Qnawsvc.CategoryTreeEntity> entities = client.GetCategoryTree();
      client.Close();

      return RenderCategoryLI(entities);

    }
    #endregion

    #region - RenderCategoryLI -
    /// <summary>
    /// Create arrays of list items LI object inside the UL created with CreateCategoryUL method.
    /// </summary>
    /// <param name="entities"></param>
    /// <returns></returns>
    private string RenderCategoryLI(IList<Qnawsvc.CategoryTreeEntity> entities)
    {
      //TODO[P3]: Implement dynamic class names. Probably take class names from config file or enum?

      string liString = "<ul class=\"qul categoryunorderedlist\">";

      foreach (Qnawsvc.CategoryTreeEntity entity in entities)
      {
        _itemNumber++;

        liString += string.Format("<li value=\"{0}\" class=\"qli listlevel{1}\">", entity.categoryID, _treeLevel.ToString());

        if (_itemWithCheckbox)
        {
          liString += string.Format("<input id=\"Category{0}\" value=\"{0}\" class=\"qcheckbox listlevel{1}\" type=\"checkbox\" name=\"CheckBoxCategories\" />",
             entity.categoryID, _treeLevel.ToString());
        }
        liString += string.Format("<a id=\"UrlToCategory{0}\" href=\"{1}\" class=\"qanchor listlevel{2}\">{3}</a>",
          entity.categoryID, _categoryUrl, _treeLevel.ToString(), entity.categoryText );

        if (entity.children.Count() > 0)
        {
          _treeLevel++;
          liString += RenderCategoryLI(entity.children);
          _treeLevel--;
        }

        liString += "</li>";

        
      }

      liString += "</ul>";
      

      return liString;

    }
    #endregion


    public string RenderCategoryModPanel()
    {
      string panel = string.Empty;

      panel += "<input type=\"text\" id=\"TextBoxCategoryText\" class=\"qtextbox\" value=\"New Category\" />";
      panel += "<input type=\"button\" id=\"\" class=\"qbutton\" value=\"Add\" />";
      panel += "<input type=\"hidden\" id=\"CategoryID\" value=\"\" />";

      panel = string.Format("<div id=\"\" class=\"categorymodpanel\">{0}</div>", panel);
      return panel;
    }
  }
}